//
//  RBGServiceAnnonce.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 31/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGServiceAnnonce.h"
#import "Constantes.h"
#import "XMLParser.h"
#import "RBGConfigurationClientService.h"


@interface RBGServiceAnnonce ()<ServiceDelegate>

@end

@implementation RBGServiceAnnonce


-(void) getAnnonces:(NSString *)url
            andPage:(int)page
       andArguments:(NSMutableDictionary *) args
{
    RBGConfigurationClientService * service = [[RBGConfigurationClientService alloc]init];
    service.delegate = self;
    
    // On récupére les paramétres
    service.typeService = [NSString stringWithFormat:@"%01d",annonces];
    
    NSMutableDictionary * arguments ;
    
    
    if(args == nil){
        arguments =[[NSMutableDictionary alloc]init];
        
        [arguments addEntriesFromDictionary:@{
                                              @"&pg" : [NSNumber numberWithInt:page]
                                              }];

    }else{
        
        [args addEntriesFromDictionary:@{
                                         @"&pg" : [NSNumber numberWithInt:page]
                                         }];
        
        arguments = args;
    }
    

    [service getDonnneesByUrl:url andArguments:arguments];
    
}

-(NSString*)recupererUrlAnnonce:(NSInteger)type{
    NSString* url = nil;

    switch (type) {
        case TYPE_ANNONCE_BATEAU_A_MOTEUR:
        case TYPE_ANNONCE_VOILIER:
        case TYPE_ANNONCE_SEMI_RIGIDE:
            url = URL_ANNONCE_DETAIL_BATEAU;
            break;
        case TYPE_ANNONCE_MOTEUR:
            url = URL_ANNONCE_DETAIL_MOTEUR;
            break;
        case TYPE_ANNONCE_ACCESSOIRE:
            url = URL_ANNONCE_DETAIL_DIVER;
            break;
        case TYPE_ANNONCE_PLACE_DE_PORT:
            url = URL_ANNONCE_DETAIL_DIVER;
            break;
        case TYPE_ANNONCE_LOCATION:
            url = URL_LOCATION_DETAIL;
            break;
        default:
            break;
    }
    return url;
}

-(void)getAnnonce:(NSString*)annonceId forType:(NSInteger)type
{
    
    RBGConfigurationClientService * service = [[RBGConfigurationClientService alloc]init];
    service.delegate = self;
    
    // On récupére les paramétres
    service.typeService = [NSString stringWithFormat:@"%01d",annonces];
    
    NSDictionary * arguments = @{
                                 @"idad" : annonceId
                                 };
    NSString *url = [self recupererUrlAnnonce:type];
    if(url != nil)
        [service getDonnneesByUrl:url andArguments:arguments];
}

-(void) getFavoris:(NSDictionary*)favoris
{
    RBGConfigurationClientService * service = [[RBGConfigurationClientService alloc]init];
    service.delegate = self;
    
    // On récupére les paramétres
    service.typeService = [NSString stringWithFormat:@"%01d",annoncesFavoris];
    
    NSMutableString *idfavorisBateau = [NSMutableString string];
    NSMutableString *idfavorismoteur = [NSMutableString string];
    NSMutableString *idfavorisdivers = [NSMutableString string];
    NSMutableString *idfavorislocation = [NSMutableString string];
    
    [favoris enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
        NSString *code = key;
        NSString *type = object;
        
        if([type isEqualToString:@"bateau" ]|| [type isEqualToString:VOILIER] || [type isEqualToString:PNEU]){
            if(idfavorisBateau.length>0)
                [idfavorisBateau appendString:@","];
            [idfavorisBateau appendString:code];
        }else if([type isEqualToString:@"moteur" ]){
            if(idfavorismoteur.length>0)
                [idfavorismoteur appendString:@","];
            [idfavorismoteur appendString:code];
        }else if([type isEqualToString:@"divers" ]){
            if(idfavorisdivers.length>0)
                [idfavorisdivers appendString:@","];
            [idfavorisdivers appendString:code];
        }else if([type isEqualToString:@"location" ]){
            if(idfavorislocation.length>0)
                [idfavorislocation appendString:@","];
            [idfavorislocation appendString:code];
        }
    }];
    
    
    NSDictionary * arguments = @{
                                 @"idfavorisbateau" : idfavorisBateau,
                                 @"idfavorismoteur" : idfavorismoteur,
                                 @"idfavorisdivers" : idfavorisdivers,
                                 @"idfavorislocation" : idfavorislocation,
                                 };
    [service getDonnneesByUrl:URL_ANNONCES_FAVORIS andArguments:arguments];
}


-(void) onDemand:(NSMutableDictionary *)arguments{
    RBGConfigurationClientService * service = [[RBGConfigurationClientService alloc]init];
    service.delegate = self;
    
    // On récupére les paramétres
    service.typeService = [NSString stringWithFormat:@"%01d",onDemand];
    
    [service getDonnneesByUrl:URL_ON_DEMAND andArguments:arguments];
}


-(void) envoieEmail:(NSMutableDictionary *)arguments{
    RBGConfigurationClientService * service = [[RBGConfigurationClientService alloc]init];
    service.delegate = self;
    
    // On récupére les paramétres
    service.typeService = [NSString stringWithFormat:@"%01d",envoieEmail];
    
    [service getDonnneesByUrl:URL_ENVOIE_EMAIL andArguments:arguments];
}


#pragma mark - ServiceDelegate

- (void) receiveDonneesService:(id)donnees fromService:(int)service{
    [self.delegate receiveDonneesAnnonce:donnees andType:service];
}

- (void) receiveErreurService:(int)statut{
    [self.delegate receiveErreurAnnonce:statut];
}



@end
